import { _decorator, Color, Component, instantiate, Label, Layout, Node, Prefab, RichText, Sprite, SpriteFrame, tween, UITransform, v3 } from 'cc';
import { Army, ArmyState } from '../entity/Army';
import { SoldierItem } from './SoldierItem';
import { Utils } from '../common/Utils';
import { UserInfo } from '../common/UserInfo';
import ArmyApi from '../api/ArmyApi';
import { UIManager } from '../common/UIManager';
import VillageApi from '../api/VillageApi';
import { EventOper } from '../entity/Buildings';
const { ccclass, property } = _decorator;

@ccclass('ArmyItem')
export class ArmyItem extends Component {

    @property(Sprite)
    cover: Sprite
    @property(RichText)
    tittle: RichText
    @property(Node)
    toggle: Node
    @property(Node)
    arrow: Node
    @property(Node)
    soldierList: Node
    @property(Label)
    conTittle: Label
    @property(Label)
    consume: Label
    @property(Node)
    back: Node
    @property(Prefab)
    soldierItem: Prefab
    @property(SpriteFrame)
    redRect: SpriteFrame

    private _layoutHeight: number = 400
    private _isOpen: boolean = true
    private _offsetTime: number = 0

    private _armyData: Army
    start() {
        this.toggle.on(Node.EventType.TOUCH_END, this.onToggle, this)
        this.back.on(Node.EventType.TOUCH_END, this.onBack, this)
    }


    setData(army: Army) {
        this._armyData = army

        this.tittle.string = army.tittle

        for (let so of army.soldierList) {
            if (so.count == 0) {
                continue
            }
            let node = instantiate(this.soldierItem)
            this.soldierList.addChild(node)
            node.getComponent(SoldierItem).setData(so)
        }





        if (army.state == ArmyState.Back) {
            this.back.active = false
        }

        if (army.state == ArmyState.Attack) {
            this.node.getComponent(Sprite).spriteFrame = this.redRect
        }

        if (army.state == ArmyState.Stay) {
            this.conTittle.string = '粮食消耗'
            this.consume.string = `${army.consume}每小时`

            if (army.zoneId == army.simZoneId) {
                this.back.active = false
            } else {

                // this.node.getComponent(Sprite).color = new Color('#213601')
            }
        } else {
            this.conTittle.string = '到达时间'

            let now = UserInfo.instance.serverTime
            now = Math.floor(now / 1000)
            this._offsetTime = army.endTime - now
            this.consume.string = Utils.secondToStr(this._offsetTime)

            if (!army.endTime) {
                return
            }

            this.schedule(this.countDown, 1, this._offsetTime - 1)


        }



    }

    countDown() {
        this._offsetTime--
        this.consume.string = Utils.secondToStr(this._offsetTime)
        if (this._offsetTime <= 0) {
            UserInfo.instance.refreshVillageDetail()
        }

    }

    onToggle() {

        this.soldierList.getComponent(Layout).resizeMode = Layout.ResizeMode.NONE
        let transform = this.soldierList.getComponent(UITransform)
        if (this._isOpen) {
            this._layoutHeight = this.soldierList.getComponent(UITransform).height
            this.soldierList.getComponent(UITransform).height = this._layoutHeight

            tween(transform)
                .to(0.2, { height: 0 })
                .start()

            this.toggle.getChildByName('Label').getComponent(Label).string = '展开'


            tween(this.arrow).to(0.2, { angle: 180 }).start()
        } else {
            tween(transform)
                .to(0.2, { height: this._layoutHeight })
                .start()
            this.toggle.getChildByName('Label').getComponent(Label).string = '收起'
            tween(this.arrow).to(0.2, { angle: 0 }).start()

        }

        this._isOpen = !this._isOpen
    }
    onBack() {
        if (this._armyData.state == ArmyState.Stay) {
            this.returnArmy()
        } else {
            this.cancelEvent()
        }
    }

    async returnArmy() {
        let params = {
            armyId: this._armyData.armyId,
        }
        for (let i = 0; i < this._armyData.soldierList.length; i++) {
            let index = i + 1
            let so = this._armyData.soldierList[i]
            params['t' + index] = so.count
        }

        await UIManager.instance.showLoading()
        let res = await ArmyApi.returnArmy(params)
        UIManager.instance.closeLoading()
        UserInfo.instance.refreshVillageDetail()
    }

    async cancelEvent() {
        await UIManager.instance.showLoading()
        let res = await VillageApi.eventOper({
            ac: EventOper.CANCEL,
            eid: this._armyData.eHandle
        })
        UIManager.instance.closeLoading()
        UserInfo.instance.refreshVillageDetail()
    }


}


